<template>
  <view class="content">
    <scroll-view scroll-y class="left-aside">
      <view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{ active: item.id === currentId }" @click="tabtap(item)">
        {{ item.name }}
      </view>
    </scroll-view>

    <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
      <view v-for="item in slist" :key="item.id" class="s-list" :id="'main-'+item.id">
        <text class="s-item">{{ item.name }}</text>
        <view class="t-list">
          <view class="t-item" v-for="titem in tlist" :key="titem.id">
             <view class="name-container">
              <text>{{ titem.name }}</text>
            </view>
            <image :src="titem.picture"></image>
            <text class="golden-text">{{ "￥" + titem.price }}</text>
            <!-- 移动预约按钮位置 -->
             <button class="reserve-btn" @click="navToList()">预约</button>
          </view>
		
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
			
			}
		},
		onLoad(){
			this.loadData();
		},
		methods: {
			async loadData(){
				let list = await this.$api.json('cateList');
				list.forEach(item=>{
					if(!item.pid){
						this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
					}else if(!item.picture){
						this.slist.push(item); //没有图的是2级分类
					}else{
						this.tlist.push(item); //3级分类
					}
				}) 
			},
			//一级分类点击
			tabtap(item){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				
				this.currentId = item.id;
				let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/product`
				})
			}
		}
	}
</script>
<style lang='scss'>
  page,
  .content {
    height: 100%;
    background-color: #f8f8f8;
  }

  .content {
    display: flex;
  }
   .golden-text {
      color: Chocolate;
    }

  .left-aside {
   flex-shrink: 0;
    width: 180upx;
    height: 100%;
    background-color: #fff;
  }

  .f-item {
    display: flex;
    align-items: left;
    justify-content: left;
    width: 100%;
    font-size: 35upx;

    color: $font-color-base;
	  margin-bottom: 200upx;
	
  }

  .f-item.active {
    color: $base-color;
    background: #f8f8f8;
  }

  .right-aside {
    flex: 1;
    overflow: hidden;
  }


  .s-item {
    display: flex;
    align-items: left;
    height: 70upx;
    font-size: 28upx;
    color: $font-color-dark;
  }

  .t-list {
  display: flex;
   flex-wrap: wrap;
 /*   width: 100%; */
    background: #fff;
/*    padding-top: 1upx; */
  }

.t-item {
  position: relative;
  flex-shrink: 0;
/*  display: flex; */
  justify-content: left;
  align-items: left;
  flex-direction: column;
  width: 100%;
  font-size: 35upx;
  color: #140;
  padding-bottom: 20upx;

  text-align: left; /* 添加这行以将文本左对齐 */

  image {
    width: 350upx;
    height: 200upx;
  }

  .name-container {
	  left:260rpx;
	  	
	  	   text-align: left; 
	  position: absolute;
	   bottom: 0;
	   right: 100;
    position: absolute;
    top: 0;
    right: 0;
	
	
  }
  .reserve-btn {
	  position:relative;
	  
	left:210rpx;
	top:-90rpx;
    width: 70px;
	display: block;
	box-sizing: border-box;
    border: 1px solid #ffcc00;
    border-radius: 50px;
    background-color: #ffcc00;
    color: #fff;
    cursor: pointer;
  }
}


 .t-item image {
   width: 250upx;
   height: 200upx;
   align-self: flex-start; /* 将图片在纵向方向上左对齐 */
 }
 .t-item text.golden-text {
	 left:260rpx;
	 top:-150rpx;
	   text-align: left; 
  position: absolute;
   bottom: 0;
   right: 100;


 }


</style>
